'use strict';

const Controller = require('egg').Controller;
const fs = require('fs');
const OSS = require('ali-oss');
const { v1 } = require('uuid');
class BaseController extends Controller {
  async fileUpload() {
    const { ctx } = this;
    let file = ctx.request.files[0];
    const fileType = file.mime.split('/')[1];
    const uuid = v1();
    const fileName = `${uuid}.${fileType}`;
    file = fs.readFileSync(file.filepath);
    const client = new OSS({
      region: 'oss-cn-beijing',
      accessKeyId: 'LTAIie6y1kuwhawN',
      accessKeySecret: 'CmidcajYAB5A0PYHM0uEvpved5JY9E',
      bucket: 'azir-bishe',
    });
    const result = await client.put(fileName, file);
    ctx.body = { code: 0, data: result };
  }
}

module.exports = BaseController;
